package com.carreirati.evaluation;


import com.carreirati.evaluation.domain.AppraisalRelationship;
import com.carreirati.evaluation.domain.AppraisalRelationshipPK;
import com.carreirati.evaluation.domain.AppraisalResults;


import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/appraisalrelationships")
@Controller
@RooWebScaffold(path = "appraisalrelationships", formBackingObject = AppraisalRelationship.class)
@RooWebJson(jsonObject = AppraisalRelationship.class)
public class AppraisalRelationshipController {

    @RequestMapping(value = "/find{appraisalId}", params = "form", produces = "text/html")
    public String find(@PathVariable("appraisalId") Long appraisalId, Model uiModel) {
        uiModel.addAttribute("appraisalrelationships", AppraisalRelationship.findAppraisalRelationshipByAppraisalId(appraisalId));
        return "appraisals/list";
    }

    
    
    
    @RequestMapping(value = "/find{appraisalId}", headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<java.lang.String> findJson(@PathVariable("appraisalId") long appraisalId) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        List<AppraisalRelationship> result = AppraisalRelationship.findAppraisalRelationshipByAppraisalId(appraisalId);
        return new ResponseEntity<String>(AppraisalRelationship.toJsonArray(result), headers, HttpStatus.OK);
    }
     
    @RequestMapping(value = "/findbyknowledgeareaid{appraisalId}", headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<java.lang.String> findbyKnowledgeAreaJson(@PathVariable("appraisalId") long appraisalId,  @RequestParam("knowledgeAreaId") long knowledgeAreaId) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        List<AppraisalRelationship> result = AppraisalRelationship.findAppraisalRelationshipByAppraisalId(appraisalId, knowledgeAreaId);
        return new ResponseEntity<String>(AppraisalRelationship.toJsonArray(result), headers, HttpStatus.OK);
    }
    
    @RequestMapping(value = "/getquestioninfo{appraisalId}", headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<java.lang.String> getQuestionInfoJson(@PathVariable("appraisalId") long appraisalId, @RequestParam("questionId") long questionId) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        List<AppraisalRelationship> result = AppraisalRelationship.findAppraisalRelationship(appraisalId, questionId);
        return new ResponseEntity<String>(AppraisalRelationship.toJsonArray(result), headers, HttpStatus.OK);
    }
     
     @RequestMapping(value =  "/update{appraisalId}", headers = "Accept=application/json")
	 @ResponseBody
	 
	 public ResponseEntity<String> updateFromJson(@PathVariable("appraisalId") long appraisalId,  @RequestParam("auxQuestion") String auxQuestion,@RequestBody String json) {
	        HttpHeaders headers = new HttpHeaders();
	        headers.add("Content-Type", "application/json; charset=utf-8");
	        AppraisalRelationship ar = AppraisalRelationship.fromJsonToAppraisalRelationship(json);	
	        
	        String[] auxQuestionInfo = auxQuestion.split(";");
	        long questionId = Long.valueOf(auxQuestionInfo[0]);        
	        int ret = ar.updateAppraisalRelationship(appraisalId, questionId, ar.getComment());
	        long[] chosenParametersId = new long[auxQuestionInfo.length -1];    
	        for (int i =1;i<auxQuestionInfo.length;i++){
	        	chosenParametersId[i-1] = Long.valueOf(auxQuestionInfo[i]);	
	        }
	        AppraisalResults appraisalResult = new AppraisalResults();     
	        appraisalResult.save(appraisalId, questionId, chosenParametersId);
	        if (ret == 1)
	        	return new ResponseEntity<String>(String.valueOf(questionId), headers, HttpStatus.OK);
	        else
	        	return new ResponseEntity<String>( headers, HttpStatus.NOT_IMPLEMENTED);
	    }
    
     @RequestMapping(value = "/updatefromarray{appraisalId}", headers = "Accept=application/json")
	 @ResponseBody
	    public ResponseEntity<String> updateFromArrayJson(@PathVariable("appraisalId") long appraisalId,  @RequestParam("auxQuestion") String auxQuestion,@RequestBody String json) {
	        HttpHeaders headers = new HttpHeaders();
	        headers.add("Content-Type", "application/json; charset=utf-8");
	        String[] questionId = auxQuestion.split(";");
	        int ret = -1;
	        AppraisalRelationship ar = new AppraisalRelationship();
	        for (int i=0; i< questionId.length; i++){
	            ret = ar.addNew(appraisalId, Long.valueOf(questionId[i]));
		        if (ret != 1)
		        	ret = -1;	        
	        }
	        if (ret == 1)	
	        	return new ResponseEntity<String>(headers, HttpStatus.OK);
	        else
	        	return new ResponseEntity<String>(headers, HttpStatus.NOT_IMPLEMENTED);
	    }
     
     @RequestMapping(value = "/deletequestion{appraisalId}", headers = "Accept=application/json")
   	 @ResponseBody
    public ResponseEntity<String> deleteFromJson(@PathVariable("appraisalId") long appraisalId,  @RequestParam("questionId") long questionId,@RequestBody String json) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");      
        AppraisalRelationship ar = new AppraisalRelationship();	
        
        int ret = ar.removeQuestion(appraisalId, questionId);
        if (ret == 1)	
        	return new ResponseEntity<String>(headers, HttpStatus.OK);
        else
        	return new ResponseEntity<String>(headers, HttpStatus.NOT_IMPLEMENTED);
    }
    
}
